#include "precompile.h"
#include "afxwinappex.h"

#include "Main.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "MainDoc.h"
#include "MainView.h"
#include "AboutDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

BEGIN_MESSAGE_MAP(CMain, CWinAppEx)
	ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinAppEx::OnFilePrintSetup)
	ON_COMMAND(ID_FILE_NEW,&CMain::OnFileNew)
END_MESSAGE_MAP()

CMain::CMain()
{
	m_bHiColorIcons = TRUE;
}

CMain theApp;
BOOL CMain::InitInstance()
{
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinAppEx::InitInstance();

	if (!LoadApplication())
		return FALSE;

	GetApplication()->Load();

	GetWorkspace()->DocumentOpening() += DocumentOpenEventHandler(this,&CMain::OnDocumentOpening);
	GetWorkspace()->DocumentCreated() += DocumentEventHandler(this,&CMain::OnDocumentCreated);
	GetWorkspace()->DocumentOpened() += DocumentEventHandler(this,&CMain::OnDocumentOpened);

	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}
	AfxEnableControlContainer();
	SetRegistryKey(_T("LiveDesign"));
	LoadStdProfileSettings(0);  
	InitContextMenuManager();
	InitShellManager();
	InitKeyboardManager();
	InitTooltipManager();

	CMFCToolTipInfo ttParams;
	ttParams.m_bVislManagerTheme = TRUE;
	theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
		RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(IDR_TEMPLATE,
		RUNTIME_CLASS(CMainDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CMainView));
	if (!pDocTemplate)
		return FALSE;
	AddDocTemplate(pDocTemplate);

	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
	{
		delete pMainFrame;
		return FALSE;
	}
	m_pMainWnd = pMainFrame;
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return TRUE;
}

int CMain::ExitInstance()
{
	if (CWinAppEx::ExitInstance() == -1)
		return -1;

	GetApplication()->Unload();
	return 0;
}

void CMain::PreLoadState()
{
	CString strName;
	strName.LoadString(IDS_EDIT_MENU);
	GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
	strName.LoadString(IDS_EXPLORER);
	GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EXPLORER);
}

void CMain::LoadCustomState()
{
}

void CMain::SaveCustomState()
{
}

/////////////////////////////////////////////////////////////////////////
//event
void CMain::OnDocumentOpened(IObject* /*sender*/,const String& docname)
{
	CMainDoc* doc =  (CMainDoc*)m_pDocManager->OpenDocumentFile(docname.c_str());
	doc->SetName(docname);

	POSITION pos = doc->GetFirstViewPosition();
	while (pos)
	{
		CMainView* view = (CMainView*)doc->GetNextView(pos);
		view->GetView()->LinkDocument(doc->GetDocument());
	}
}

void CMain::OnDocumentOpening(IObject* /*sender*/,const String& docname)
{
	CString filename;
	POSITION pos = m_pDocManager->GetFirstDocTemplatePosition();
	CDocTemplate*  doctemplate = m_pDocManager->GetNextDocTemplate(pos);
	if (DoPromptFileName(filename,AFX_IDS_OPENFILE,NULL,TRUE,doctemplate))
	{
		String& tempdocname = const_cast<String&>(docname);
		tempdocname = filename;
	}
}

void CMain::OnDocumentCreated(IObject* /*sender*/,const String& docname)
{
	POSITION pos = m_pDocManager->GetFirstDocTemplatePosition();
	CDocTemplate*  doctemplate = m_pDocManager->GetNextDocTemplate(pos);
	CMainDoc* doc =  (CMainDoc*)doctemplate->OpenDocumentFile(NULL);
	doc->SetName(docname);

	pos = doc->GetFirstViewPosition();
	while (pos)
	{
		CMainView* view = (CMainView*)doc->GetNextView(pos);
		view->GetView()->LinkDocument(doc->GetDocument());
	}
}

bool CMain::LoadApplication()
{
	HMODULE hApplication = LoadLibrary(L"Workspace.xdll");
	return hApplication!=NULL;
}

void CMain::OnFileNew()
{
	GetApplication()->GetController()->ExecuteCommand(Commands::NewFile);
}